package com.vf.admin.client.dao;

import com.vf.admin.client.dto.AppTuanDuiDto;
import com.vf.admin.client.dto.InvitationRankDto;
import com.vf.admin.client.dto.Invite3Dto;
import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppInvitationRecord;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 邀请
 */
@Repository
public class AppInvitationRecordDao extends BaseDao<AppInvitationRecord> {

    /**
     * 详情
     */
    public AppInvitationRecord get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_invitation_record where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppInvitationRecord.class);
    }

    /**
     * 根据用户id获取邀请人
     */
    public AppInvitationRecord getShangji(Integer userid) {
        if (StringEx.isNullOrLing(userid)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_invitation_record where 1=1");
        sbStr.append(" and userid = ? ");
        paramList.add(userid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppInvitationRecord.class);
    }

    /**
     * 根据状态筛选未发放奖励的邀请信息
     */
    public List<AppInvitationRecord> trackInviteSent(Integer status) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_invitation_record where 1=1");
        sbStr.append(" and status = ? ");
        paramList.add(status);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppInvitationRecord.class);
    }


    /**
     * 添加/修改
     */
    public void save(AppInvitationRecord haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }


    /**
     * 后天管理获取所有邀请信息
     *
     * @param map
     * @param bean
     * @return
     */
    public List<AppInvitationRecord> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT * FROM app_invitation_record  where 1=1 ");
        if (StringEx.isNotNull(map.get("nick"))) {
            sbStr.append(" and nick like ? ");
            paramList.add("%" + map.get("nick") + "%");
        }
        if (StringEx.isNotNull(map.get("phone"))) {
            sbStr.append(" and phone like ? ");
            paramList.add("%" + map.get("phone") + "%");
        }
        if (!StringEx.isNull(map.get("startDate"))) {
            sbStr.append(" and createtime>=? ");
            paramList.add(map.get("startDate"));
        }
        if (!StringEx.isNull(map.get("endDate"))) {
            sbStr.append(" and createtime<=? ");
            paramList.add(map.get("endDate"));
        }
        sbStr.append(" order by createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppInvitationRecord.class, bean);
    }


    /**
     * 更新用户头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateImg(Integer userid, String pic) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_invitation_record SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户昵称
     *
     * @param userid
     * @param nick   昵称
     */
    public void updateNick(Integer userid, String nick) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_invitation_record SET nick = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新上级头像
     *
     * @param userid
     * @param pic    头像地址
     */
    public void updateSjImg(Integer userid, String pic) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_invitation_record SET ppic = ?  WHERE puserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新上级昵称
     *
     * @param userid
     * @param nick   昵称
     */
    public void updateSjNick(Integer userid, String nick) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(nick)) {
            return;
        }
        String sql = " UPDATE app_invitation_record SET pnick = ?  WHERE puserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(nick);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 查看我的邀请信息
     *
     * @Param: [userid]
     * @return: java.util.List<com.vf.admin.miaodong.entity.AppInvitationRecord>
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 11:37
     */
    public List<AppInvitationRecord> findByYaoqingList(Integer userid) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT * FROM app_invitation_record  where 1=1 ");
        if (StringEx.isNotNull(userid)) {
            sbStr.append(" and puserid  = ? ");
            paramList.add(userid);
        }
        sbStr.append(" order by createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppInvitationRecord.class);
    }

    /**
     * 获取我的奖金
     *
     * @Param: [userid]
     * @return: com.vf.admin.miaodong.entity.AppInvitationRecord
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 12:58
     */
    public AppInvitationRecord getJiangjin(Integer userid) {
        if (StringEx.isNullOrLing(userid)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder(" select SUM(supermoney) AS jiangjin from app_invitation_record where 1=1");
        sbStr.append(" and puserid = ? ");
        paramList.add(userid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppInvitationRecord.class);
    }

    /**
     * 查看我的团队
     *
     * @Param: [userid, splitPageBean]
     * @return: java.util.List<com.vf.admin.miaodong.entity.AppInvitationRecord>
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 16:44
     */
    public List<AppTuanDuiDto> findByTuandui(Integer userid, SplitPageBean splitPageBean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("  SELECT id,userId,createtime,supermoney,pic,nick,phone,sex,ppic,pnick,pphone,ppsex FROM app_invitation_record  WHERE 1=1  ");
        if (StringEx.isNotNull(userid)) {
            sbStr.append(" and puserid  = ? ");
            paramList.add(userid);
        }
        sbStr.append(" order by createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppTuanDuiDto.class, splitPageBean);
    }

    /**
     * 更新上级用户性别
     *
     * @Param: [userid, sex]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 18:18
     */
    public void updateSjSex(Integer userid, Integer sex) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(sex)) {
            return;
        }
        String sql = "  UPDATE app_invitation_record SET ppsex = ?  WHERE puserid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更新用户性别
     *
     * @Param: [userid, sex]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/28
     * @Time: 18:18
     */
    public void updateSex(Integer userid, Integer sex) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(sex)) {
            return;
        }
        String sql = " UPDATE app_invitation_record SET sex = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(sex);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    /**
     * 更改头像
     *
     * @Param: [userid, pic]
     * @return: void
     * @Author: zhouyunyi
     * @Date: 2021/07/5
     * @Time: 18:43
     */
    public void updatePic(Integer userid, String pic) {
        if (StringEx.isNull(userid)) {
            return;
        }
        if (StringEx.isNull(pic)) {
            return;
        }
        String sql = " UPDATE app_invitation_record SET pic = ?  WHERE userid = ? ";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(pic);
        paramList.add(userid);
        this.update(sql, paramList);
    }

    public AppInvitationRecord selectBind(Integer userId) {
        StringBuilder str = new StringBuilder();
        List list = new ArrayList();
        str.append("select * from app_invitation_record where userid = ?");
        list.add(userId);
        String sql = str.toString();
        return this.queryObj(sql, list, AppInvitationRecord.class);
    }

    public List<Invite3Dto> invite3() {
        StringBuilder str = new StringBuilder();
        str.append("select sum(supermoney) as money, puserid as userId from app_invitation_record group by puserid order by money desc");
        String sql = str.toString();
        return this.queryList(sql, new ArrayList(), Invite3Dto.class);
    }

    public void delByUserId(Integer userId) {
        ArrayList<Object> params = new ArrayList<>();
        StringBuilder str = new StringBuilder();
        str.append("delete from app_invitation_record where userid = ? or puserid = ?");
        params.add(userId);
        params.add(userId);
        String sql = str.toString();
        del(sql, params);
    }

    public List<InvitationRankDto> inviteRank() {
        StringBuilder str = new StringBuilder();
        str.append("select sum(supermoney) as money, puserid as userId,pnick as nick,ppic as pic from app_invitation_record group by puserid order by money desc ");
        String sql = str.toString();
        return this.queryList(sql, new ArrayList(), InvitationRankDto.class);
    }
}
